from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import unittest

class NewVisitorTest(unittest.TestCase):

	def setUp(self):
		self.browser = webdriver.Chrome()
		self.browser.implicitly_wait(3)
	
	def tearDwon(self):
		self.browser.quit()
	
	def check_for_row_in_list_table(self, row_text):
		table = self.browser.find_element_by_id('id_list_table')
		rows = table.find_elements_by_tag_name('tr')
		self.assertIn(row_text, [row.text for row in rows])

	def test_can_start_a_list_and_retrieve_it_later(self):
		self.browser.get('http://localhost:8000')

		self.assertIn('To-Do',self.browser.title)
		header_text = self.browser.find_element_by_tag_name('h1').text
		self.assertIn('To-Do',header_text)

		inputbox = self.browser.find_element_by_id('id_new_item')
		self.assertEqual(
			inputbox.get_attribute('placeholder'),
			'Enter a to-do item'
		)

		inputbox.send_keys('Buy peacock feathers')
		
		# 待办事项表格中显示了“1： Buy peacock feathers”
		inputbox.send_keys(Keys.ENTER)
		self.check_for_row_in_list_table('1: Buy peacock feathers')
		
		# 页面中还有一个文本框，可以输入其他的待办事项：
		# 她输入了“Use peacock feathers to make a fly”（使用孔雀羽毛做假蝇）
		# 伊迪丝做事很有条理
		inputbox = self.browser.find_element_by_id('id_new_item')
		inputbox.send_keys('Use peacock feathers to make a fly')
		inputbox.send_keys(Keys.ENTER)

		self.check_for_row_in_list_table('1: Buy peacock feathers')
		self.check_for_row_in_list_table('2: Use peacock feathers to make a fly')


       ## 页面再次更新，清单中显示了这两个待办事项
		table = self.browser.find_element_by_id('id_list_table')
		rows = table.find_elements_by_tag_name('tr')

#		self.assertTrue(
#			any(row.text == '1:Buy peacock feathers' for row in rows),
#			"New to-do item did not appear in table -- its text was:\n%s"%(
#				table.text,
#			)
#		)

		self.assertIn('1: Buy peacock feathers',[row.text for row in rows])
		self.assertIn(
			'2: User peacock feathers to make a fly',
			[row.text for row in rows]
		)
		

		# 伊迪丝想知道这个网站是否会记住她的清单
		# 她看到网站为她生成了一个唯一的URL
		# 页面中有一些文字解说这个功能
		self.fail('Finish the test!')

		# 她访问那个URL，发现待办事项清单还在
if __name__ == '__main__':
	unittest.main(warnings='ignore') 
